Conditions | 1 |
Paths | 1 |
Total Lines | 110 |
Code Lines | 70 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** global: Craft */ |
||
3 | $(function () { |
||
4 | |||
5 | var $form = $('.cacheFlag-form'); |
||
6 | var $submitRequest = null; |
||
7 | |||
8 | $form |
||
9 | .on('submit', onFormSubmit) |
||
10 | .on('click', '[data-clearflags]', clearCaches); |
||
11 | |||
12 | function onFormSubmit(e) { |
||
13 | e.preventDefault(); |
||
14 | submitForm(); |
||
15 | } |
||
16 | |||
17 | function submitForm() { |
||
18 | |||
19 | if ($submitRequest) { |
||
20 | $submitRequest.abort(); |
||
21 | } |
||
22 | |||
23 | $form.addClass('js-submitting'); |
||
24 | $form.find('.spinner').removeClass('hidden'); |
||
25 | $form.find('input[type="submit"]').prop('disabled', true).addClass('disabled'); |
||
26 | |||
27 | $submitRequest = $.ajax($form.attr('action'), { |
||
28 | data: $form.serialize(), |
||
29 | type: 'POST', |
||
30 | dataType: 'json', |
||
31 | success: function (response) { |
||
32 | if (response.success) { |
||
33 | Craft.cp.displayNotice(response.message); |
||
34 | $form.find('input[type="text"][name^="cacheflags"]').each(function () { |
||
35 | var $input = $(this); |
||
36 | var source = $input.attr('name').replace('cacheflags[', '').replace(']', '').split(':'); |
||
37 | var sourceColumn = source[0] || null; |
||
38 | var sourceValue = (source[1] || '').toString(); |
||
39 | if (!sourceColumn || !sourceValue) { |
||
40 | return; |
||
41 | } |
||
42 | var flags = ''; |
||
43 | for (var i = 0; i < response.flags.length; ++i) { |
||
44 | if ((response.flags[i][sourceColumn] || '').toString() === sourceValue) { |
||
45 | flags = response.flags[i].flags || ''; |
||
46 | break; |
||
47 | } |
||
48 | } |
||
49 | $input.val(flags); |
||
50 | }); |
||
51 | } else { |
||
52 | Craft.cp.displayError(response.message); |
||
53 | } |
||
54 | }, |
||
55 | error: function (response) { |
||
56 | if (response.statusText !== 'abort') { |
||
57 | Craft.cp.displayError(response.statusText); |
||
58 | } |
||
59 | }, |
||
60 | complete: function () { |
||
61 | $submitRequest = null; |
||
62 | $form.removeClass('js-submitting'); |
||
63 | $form.find('.spinner').addClass('hidden'); |
||
64 | $form.find('input[type="submit"]').prop('disabled', false).removeClass('disabled'); |
||
65 | } |
||
66 | }); |
||
67 | |||
68 | } |
||
69 | |||
70 | function clearCaches(e) { |
||
71 | |||
72 | e.preventDefault(); |
||
73 | |||
74 | var actionUrl = Craft.getActionUrl('cache-flag/default/delete-flagged-caches-by-flags'), |
||
75 | $target = $(e.currentTarget), |
||
76 | flags = $target.data('clearflags'); |
||
77 | |||
78 | if ($target.hasClass('disabled') || !flags || flags == '') { |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | var data = { |
||
83 | flags: flags |
||
84 | }; |
||
85 | |||
86 | data[$form.data('csrf-name')] = $form.data('csrf-token'); |
||
87 | |||
88 | $target.addClass('disabled'); |
||
89 | |||
90 | $.ajax(actionUrl, { |
||
91 | type: 'POST', |
||
92 | data: data, |
||
93 | dataType: 'json', |
||
94 | success: function (response) { |
||
95 | if (response.success) { |
||
96 | Craft.cp.displayNotice(response.message); |
||
97 | } else { |
||
98 | Craft.cp.displayError(response.message); |
||
99 | $target.removeClass('disabled'); |
||
100 | } |
||
101 | }, |
||
102 | error: function (response) { |
||
103 | if (response.statusText !== 'abort') { |
||
104 | Craft.cp.displayError(response.statusText); |
||
105 | } |
||
106 | $target.removeClass('disabled'); |
||
107 | } |
||
108 | }); |
||
109 | |||
110 | } |
||
111 | |||
112 | }); |
||
113 |